/**
 * 
 */
package cn.egame.extraction;

import java.util.ArrayList;
import java.util.List;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.log4j.Logger;

import cn.egame.extraction.pojo.ProcessBean;
import cn.egame.extraction.util.ConstVar_EFS;
import cn.egame.extraction.util.FileOperator;

/**
 * Description 文件拉取程序（定时同步文件）
 * 
 * @ClassName FileSynchronizeSchduler
 * 
 * @Copyright 炫彩互动
 * 
 * @Project egame.mobile.extraction
 * 
 * @Author yujm
 * 
 * @Create Date 2014-12-05
 * 
 * @Modified by none
 * 
 * @Modified Date
 */

public class FileSynchronizeSchduler extends Thread {
    private static final Logger logger = Logger.getLogger("egameTimeSync");
    public static String READ_FILE_NAME = "";
    public static List<ProcessBean> processList = new ArrayList<ProcessBean>();
    public static AtomicBoolean flag = new AtomicBoolean(false);
    public FileSynchronizeSchduler() {

    }

    @Override
    public void run() {
        flag.set(false);;
        // 1.读取文件到list
        long start = System.currentTimeMillis();
        init();
        // 2.同步文件
        for (ProcessBean  bean : processList) {
            FileOperator.synFile(bean.getTarget(),bean.getSource(), READ_FILE_NAME);
        }
        // 3.同步程序结束
        logger.info("####文件同步完成！");
        long end = System.currentTimeMillis();
        logger.info("######Total spend time: " + (end - start) / 1000.0 + "s");
        try {
            Thread.sleep(ConstVar_EFS.PROCESS_PER_TIME);
        } catch (InterruptedException e) {
       
            logger.error(e.getMessage(), e);
        }
        flag.set(true);
    }

    /**
     * @Description: 程序初始化加载数据
     * 
     * @return void
     * @throws
     * @Author yujm
     * @Create Date 2014-12-05
     * @Modified by none
     * @Modified Date
     */
    private void init() {
        try {
            FileSynchronizeSchduler.processList.clear();
            long start = System.currentTimeMillis();
            // 读取配置文件
            READ_FILE_NAME = System.currentTimeMillis() + ".txt";
            String[] sourceArr = ConstVar_EFS.TXT_SOURCE.split(",");
            String[] targetArr = ConstVar_EFS.TXT_TARGET.split(",");
            if (sourceArr.length != 0) {
                for (int i = 0; i < sourceArr.length; i++) {
                    processList.add(new ProcessBean(sourceArr[i],targetArr[i]));
                }
            }
            for (ProcessBean bean : processList) {

                FileOperator.writeFileIndex(bean, READ_FILE_NAME);
            }
            long end = System.currentTimeMillis();
            logger.info("######init spend time: " + (end - start) / 1000.0 + "s");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

    }

}
